import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by zzf on 2017/4/25.
 */
public class BlockClient {
    private static AtomicInteger number = new AtomicInteger();
    public static void main(String[] args) {

        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                Socket client = null;
                DataOutputStream out = null;
                DataInputStream in = null;
                try {
                    client = new Socket("127.0.0.1", 9999);
                    client.setSoTimeout(10000);
                    out = new DataOutputStream( (client.getOutputStream()));

                    String query = number.toString();
                    number.incrementAndGet();
                    byte[] request = query.getBytes("utf-8");
                    out.write(request);
                    out.flush();
                    client.shutdownOutput();

                    in = new DataInputStream(client.getInputStream());
                    byte[] reply = new byte[40];
                    in.read(reply);
                    System.out.println("Time: " + new String(reply, "utf-8"));

                    in.close();
                    out.close();
                    client.close();
                }
                catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        };

        for (int i=0;i<10;i++) {
            new Thread(runnable).start();
        }

//        new Thread(runnable).start();
//        new Thread(runnable).start();
//        new Thread(runnable).start();
//        new Thread(runnable).start();
//        new Thread(runnable).start();
//        new Thread(runnable).start();
//        new Thread(runnable).start();


    }
}
